﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;


namespace opSysPower.Definition
{
    public interface IGraphicManager<T> 
    {
        List<T> SchematicList { get; }
        void Add(T t);
        void Remove(T t);
        T this[int index] { get; set; }

    }

    public interface ISchematicManager : IGraphicManager<ISchematic>
    {
        ISchematic this[UInt32 identifier] { get; }
    }

    
    public abstract class GraphicManager<T> : IGraphicManager<T> where T : IGraphicSchematic
    {

        private List<T> _internalList;

        public GraphicManager()
        {
            _internalList = new List<T>();
        }
   

        #region IGraphicManager<T> 成员

        public List<T> SchematicList
        {
            get { return _internalList; }
        }

        public void Add(T t)
        {
           _internalList.Add(t);
        }

        public void Remove(T t)
        {
            _internalList.Remove(t);
        }

        #endregion

        #region IGraphicManager<T> 成员


        public T this[int index]
        {
            get
            {
                if(index<_internalList.Count && index>=0)
                {
                    return _internalList[index];
                }

                throw new ArgumentOutOfRangeException("索引越界");
            }
            set
            {
                if (index < _internalList.Count && index >= 0)
                {
                    _internalList[index] = value;
                }

                throw new ArgumentOutOfRangeException("索引越界");
            }
        }

        #endregion
    }

    [Export(typeof(ISchematicManager))]
    public class SchematicManager:GraphicManager<ISchematic>,ISchematicManager
    {

        #region ISchematicManager 成员

        public ISchematic this[UInt32 identifier]
        {
            get { return SchematicList.FirstOrDefault(s => s.Value == identifier); }
        }

        #endregion
    }

}
